package com.chuangyang.fixboxmaster.ui.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.R;

/**
 * Created by Administrator on 2015/7/30.
 */
public class SelectListItem  extends LinearLayout {

    private TextView listItemDescriptionL;
    private TextView listItemDescriptionR;

    private void init(){
        setClickable(true);
        setFocusable(true);
    }


    private void drawView(Context context,AttributeSet attrs){
        TypedArray a = null;
        View view ;
        try {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.select_list_item, this, true);
            //ImageView listItemLeftIcon = (ImageView) view.findViewById(R.id.list_item_left_icon);
            listItemDescriptionL = (TextView) view.findViewById(R.id.dec_left);
            listItemDescriptionR = (TextView) view.findViewById(R.id.dec_right);
            ImageView listItemRightIcon = (ImageView) view.findViewById(R.id.src_right);
            a = context.obtainStyledAttributes(attrs, R.styleable.SelectListItemView);
            String descriptionLeft =a.getString(R.styleable.SelectListItemView_item_left_desc);
            String descriptionRight =a.getString(R.styleable.SelectListItemView_item_right_desc);
            Drawable srcRight = a.getDrawable(R.styleable.SelectListItemView_item_right_src);
            if(srcRight != null){
                listItemRightIcon.setVisibility(VISIBLE);
                listItemRightIcon.setImageDrawable(srcRight);
            }
            if(descriptionLeft != null){
                listItemDescriptionL.setVisibility(VISIBLE);
                listItemDescriptionL.setText(descriptionLeft);
            }
            if(descriptionRight != null){
                listItemDescriptionR.setVisibility(VISIBLE);
                listItemDescriptionR.setText(descriptionRight);
            }

        } catch (Exception e) {
        } finally {
            if (a != null)
                a.recycle();
        }
    }

    public void setLDescription(String desc){
        if(desc != null){
            listItemDescriptionL.setVisibility(VISIBLE);
            listItemDescriptionL.setText(desc);
        }
    }

    public void setRDescription(String desc){
        if(desc != null){
            listItemDescriptionR.setVisibility(VISIBLE);
            listItemDescriptionR.setText(desc);
        }
    }

    public String getLDescription(){
        if(listItemDescriptionL != null){
            return listItemDescriptionL.getText().toString();
        }
        return  "";
    }

    public String getRDescription(){
        if(listItemDescriptionR != null){
            return listItemDescriptionR.getText().toString();
        }
        return  "";
    }

    public SelectListItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        drawView(context,attrs);
    }
}
